package com.sunnada.nginx.bean.nginx.http;

import com.alibaba.fastjson.JSON;
import com.github.odiszapc.nginxparser.NgxBlock;
import com.github.odiszapc.nginxparser.NgxEntry;
import com.github.odiszapc.nginxparser.NgxParam;
import com.github.odiszapc.nginxparser.NgxToken;
import com.sunnada.nginx.common.NginxUtils;
import com.sunnada.nginx.exception.CustonException;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

@Data
public class HttpServer {
    @ApiModelProperty(hidden = true)
    private String nameAndPort;
    @ApiModelProperty(value = "服务器名称")
    private List<String> names = new ArrayList<>();
    @ApiModelProperty(value = "服务器接口")
    private String port;
    @ApiModelProperty(hidden = true)
    private List<Location> locations = new ArrayList<>();

    public HttpServer() {
    }

    public HttpServer(NgxBlock ngxBlock) {
        Collection<NgxEntry> entries = ngxBlock.getEntries();
        for (NgxEntry entry : entries) {
            if (!(entry instanceof NgxParam || entry instanceof NgxBlock)) continue;
            if (entry instanceof NgxParam){
                NgxParam param = (NgxParam) entry;
                if ("listen".equals(param.getName())) {
                    port = param.getValue();
                    continue;
                }
                if ("server_name".equals(param.getName())) {
                    Collections.addAll(names, param.getValue().split(" "));
                    continue;
                }
            }
            if(entry instanceof NgxBlock){
                NgxBlock block = (NgxBlock)entry;
                if ("location".equals(block.getName())){
                    locations.add(new Location(names.size()==0?"":names.get(0),port,block));
                }
            }
        }
        if (StringUtils.isBlank(port)) port = "80";
        if (names.size() == 0) names.add("");
        nameAndPort = new StringBuffer(names.get(0)).append(":").append(port).toString();
    }

    /**
     * 获取使用serverName与port生成的nameAndPort
     * @return
     */
    public String getNewNameAndPort(){
        if (names.size() == 0) names.add("");
        return new StringBuffer(names.get(0)).append(":").append(port).toString();
    }

    public String toJson() {
        return JSON.toJSONString(this);
    }

    /**
     * 将对象转为NgxBlock对象
     * @return
     */
    public NgxBlock toNgxBlock(){
        NgxBlock block = new NgxBlock();
        block.addValue(new NgxToken(getToken()));
        block.addEntry(portToNgxParam());
        block.addEntry(nameToNgxParam());
        return block;
    }

    private NgxParam portToNgxParam(){
        NgxParam ngxParam = new NgxParam();
        ngxParam.addValue("listen");
        ngxParam.addValue(port);
        return ngxParam;
    }

    private NgxParam nameToNgxParam(){
        NgxParam ngxParam = new NgxParam();
        ngxParam.addValue("server_name");
        for (String name : names) {
            ngxParam.addValue(name);
        }
        return ngxParam;
    }

    public String getToken(){
        return "server";
    }

    /**
     * 校验这个对象的有效性
     * @return
     */
    public boolean vali() throws CustonException {
        if (StringUtils.isBlank(port)) throw new CustonException("接口不能为空");
        return true;
    }
}
